package com.shoppingmall.common;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.io.Serializable;

/**
 * 定义一个通用的服务返回对象
 * Created by Administrator on 2019/3/18 13:04
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ServletResponse<T> implements Serializable {
    private int status;
    private String msg;
    private T data;

    private ServletResponse(int status){
        this.status =status;

    }
    private ServletResponse(int status,T data){
        this.status = status;
        this.data = data;
    }



    private ServletResponse(int status,String msg){

        this.status=status;
        this.msg =msg;
    }

    private ServletResponse(int status,String msg,T data){

        this.msg =msg;
        this.status = status;
        this.data =data;
    }
    @JsonIgnore
    public boolean isSuccess(){
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    public int getStatus(){
        return status;
    }
    public T getData(){
        return data;
    }
    public  String getMsg(){
        return msg;

    }

    public static <T> ServletResponse<T> createBySuccess(){

        return new ServletResponse<T>(ResponseCode.SUCCESS.getCode());
    }
    public static <T> ServletResponse<T> createBySuccessMessage(String msg){
        return new ServletResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }
    public static <T> ServletResponse<T> createBySuccess(T data){

        return new ServletResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public  static <T> ServletResponse<T> createBySuccess(String msg,T data){

        return new ServletResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }

    public static <T> ServletResponse<T> createByError(){
        return new ServletResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());

    }

    public static <T> ServletResponse<T> createByErrorMessage(String errorMessage){

        return new ServletResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
    }
    public static <T> ServletResponse<T> createByErrorcodeAndMessage(int errorCode,String message){
        return new ServletResponse<T>(errorCode,message);
    }

    public static <T> ServletResponse<T> createByErrorMessageAndData(String msg,T data){

        return new ServletResponse<T>(ResponseCode.ERROR.getCode(),msg,data);
    }


















}
